package com.mask.im.business.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.Feedback;
import com.mask.im.business.common.exception.BusinessException;
import com.mask.im.business.mapper.FeedbackMapper;
import com.mask.im.business.model.dto.FeedbackDTO;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.model.vo.FeedbackVO;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.FeedbackService;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import com.mask.im.business.common.util.PageUtil;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;


@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

	@Override
	public RestResponse<String> saveFeedback(FeedbackVO feedbackVO) {
		if (feedbackVO.getFeedbackTypeId()==null || feedbackVO.getContent()==null){
			throw new BusinessException("请检查是否已完善了必填项");
		}
		Feedback feedback = Feedback.builder().content(feedbackVO.getContent())
				.feedbackImageUrl(feedbackVO.getFeedbackImageUrl() != null ? String.join(",", feedbackVO.getFeedbackImageUrl()) : null)
				.status(0)
				.userId(MaskIMSecurityUtils.getUserId())
				.feedbackTypeId(feedbackVO.getFeedbackTypeId())
				.build();
		int insert = this.baseMapper.insert(feedback);
		if (insert>0){
			return RestResponse.ok();
		}else{
			return RestResponse.fail();
		}
	}

	@SneakyThrows
	@Override
	public PageResult<FeedbackDTO> feedbackList(ConditionVO conditionVO) {
		CompletableFuture<Integer> feedbackCount = CompletableFuture.supplyAsync(() ->
				this.baseMapper.selectFeedbackCount(conditionVO));
		List<FeedbackDTO> feedbackDTOS =
				this.baseMapper.feedbackList(conditionVO, PageUtil.getCurrentPage(),PageUtil.getSize());
		return PageResult.<FeedbackDTO>builder().data(feedbackDTOS).total(feedbackCount.get()).build();
	}

	@Override
	public RestResponse<String> refuseAccept(Integer feedbackId, String refuseReason) {
		Feedback feedback = Feedback.builder()
				.id(feedbackId)
				.reason(refuseReason)
				.processors(MaskIMSecurityUtils.getUserId())
				.status(3)
				.build();
		boolean update = this.updateById(feedback);
		//TODO 邮箱通知反馈进程
		if (update){
			return RestResponse.ok("拒绝受理成功");
		}
			return RestResponse.fail("拒绝受理失败");
	}

	@Override
	public RestResponse<String> statusChanges(Integer feedbackId,Integer status) {
		Feedback feedback = Feedback.builder()
				.id(feedbackId)
				.processors(MaskIMSecurityUtils.getUserId())
				.status(status)
				.build();
		boolean update = this.updateById(feedback);
		switch (status){
			case 1: //处理中
				//TODO 通知用户反馈进度
			case 2: //处理完成
				//todo 通知用户反馈进度
			case 3: //异常

		}
		if (update){
			return RestResponse.ok();
		}
		return RestResponse.fail();
	}

	@Override
	public RestResponse<String> feedbackReason(Integer feedbackId) {
		Feedback byId = this.getById(feedbackId);
		return RestResponse.ok(byId.getReason());
	}

	@Override
	public RestResponse<String> delete(Integer id) {
		int delete = this.baseMapper.deleteById(id);
		if (delete>0){
			return RestResponse.ok("删除成功");
		}
		return RestResponse.fail("删除失败");
	}
}

